package com.ep.thirdPlatformProject.system.util;


import com.ep.thirdPlatformProject.job.model.MailBean;
import com.ep.thirdPlatformProject.system.config.oa.WorkflowFormProperties;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
public class MailUtil {


    private final WorkflowFormProperties workflowFormProperties;


    private final MailProperties mailProperties;


    private final JavaMailSender javaMailSender;//注入QQ发送邮件的bean


    private final static String to_mail = "huyutao@ep-ep.com";

    /**
     * 发送文本邮件
     *
     * @param mailBean 邮件实体
     */
    public void sendSimpleMail(MailBean mailBean) {
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setFrom(mailProperties.getUsername());//发送者
            mailMessage.setTo(mailBean.getRecipient());//接收者
            mailMessage.setSubject(mailBean.getSubject());//邮件标题
            mailMessage.setText(mailBean.getContent());//邮件内容
            if (workflowFormProperties.getIsSendEmail()){
                javaMailSender.send(mailMessage);//发送邮箱
            }
        } catch (Exception e) {
            log.error("邮件发送失败：{}", e.getMessage(), e);
        }
    }


    public void sendSimpleMail(String subject, String text) {
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(mailProperties.getUsername());
            helper.setTo(to_mail);
            helper.setSubject(subject);
            helper.setText(text);
            if (workflowFormProperties.getIsSendEmail()){
                javaMailSender.send(message);//发送邮箱
            }
        } catch (Exception e) {
            log.error("邮件发送失败：{}", e.getMessage(), e);
        }
    }
}
